<?php

class zhou_simaModel extends Model {

    // 获取路线分类
    public function get_cate_all($table,$where){
        $sql = 'select * from '.$this->prefix .$table.' where '.$where;
        return $this->db->query($sql)->result_array();
    }
    // 获取一条记录
    public function get_one($table,$where){
        $sql = "select * from ".$this->prefix .$table." where ".$where;
        return $this->db->query($sql)->row_array();
    }

    //插入到数据表
    public function db_add($table,$data){
        $res=$this->db->insert($table,$data);//将$data插入数据库的表中
        if (!$res) {
            return false;
        }
        return true;
    }

    //清空 数据表 
    public function db_truncate($table){
        $sql= " truncate table ".$this->prefix.$table;
        return $res = $this->db->query($sql);
        if (!$res) {
            return false;
        }
            return true;
    }
    
    //修改数据库表
    public function update_table($table,$data,$where){
        $res=$this->db->update($table,$data,$where);
        if ($res!==false) {
            return true;
        }
        return false;
    }
    public function copy_one_data($table,$clount,$where){
        $clounts = implode(',',$clount);
        $sql = "insert into ".$this->prefix."$table($clounts) select $clounts from ".$this->prefix."$table where $where";
        $res = $this->db->query($sql);
        if ($res) {
            return true;
        }
        return false;
    }
    //获取路线的全部信息
    public function get_route_news_all($table,$where,$member_id){
        $sql = "select * from ".$this->prefix.$table ." left join  ".$this->prefix."member on ".$this->prefix."member.id = ".$this->prefix.$table.".member_id ". " where  $where";
        return $this->db->query($sql)->result_array();
    }
    // 获取一条记录
    public function get_route_news_one($table,$member_id){
        $sql = "select * from ".$this->prefix.$table ." left join  ".$this->prefix."member on ".$this->prefix."member.id = ".$this->prefix.$table.".member_id ". "where route_show = 1 order by route_addtime desc";
        return $this->db->query($sql)->row_array();
    }

        //个人认证编辑
    public function get_route_user_one($table,$where){
        // $sql = "select * from ".$this->prefix.$table ." as l left join  ".$this->prefix."member  where l.real_u_id = m.id ";
        $sql = "select * from ".$this->prefix.$table." as l left join ".$this->prefix."member as m on l.real_u_id=m.id where ".$where;
        // var_dump($sql);
        return $this->db->query($sql)->row_array();
    }
    public function del($table,$where) 
    {
        $res = $this->db->delete($table,$where);
        if (!$res) {
            return false;
        }
        return true;
    }
    //连表查询获取单条信息
    public function get_join_cate_one($table,$where,$join_table,$join_where){
        $sql = 'select * from '.$this->prefix .$table.' left join '.$this->prefix.$join_table. ' on '.$join_where. ' where '.$where;
        return $this->db->query($sql)->row_array();
    }
    //连表查询获取全部信息
    public function get_join_cate_all($table,$where,$join_table,$join_where){
        $sql = 'select * from '.$this->prefix .$table.' left join '.$this->prefix.$join_table. ' on '.$join_where. ' where '.$where;
        return $this->db->query($sql)->result_array();
    }
    // 查询当天的记录
    public function get_today_count(){
        $sql = "select * from `fn_lei_zmcx_route_news` where date_format(from_UNIXTIME(`route_addtime`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')";
        return $this->db->query($sql)->result_array();
 
    }
    // 我今天发布的数量
    public function get_route_count($u_id){
        $sql = "select * from `fn_lei_zmcx_route_news` where date_format(from_UNIXTIME(`route_addtime`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') AND car_type = 3 AND pub_u_id = $u_id";
        return count($this->db->query($sql)->result_array());
 
    }
    // 查询当天的记录
    public function get_month_count(){
        $sql = "select * from `fn_lei_zmcx_route_news` where date_format(from_UNIXTIME(`route_addtime`),'%Y-%m') = date_format(now(),'%Y-%m')";
        return $this->db->query($sql)->result_array();
 
    }
    public function db_add_re_insert_id($table,$data){
        $res=$this->db->insert($table,$data);//将$data插入数据库的表中
        if (!$res) {
            return false;
        }
        return $this->get_insert_id();
    }
    public function get_page($page,$pageshow,$pagecount,$table,$where,$join_table,$join_where){  
        $pagesize = ($page-1) * $pageshow;  
        //计算总页数:  
        $numpages = ceil($pagecount/$pageshow);   //向上取整； 
        $sql = "SELECT * FROM ".$this->prefix."$table  left join ".$this->prefix.$join_table." on  $join_where  where  $where limit $pagesize,$pageshow";
        // $this->db->query($sql)->result_array();
        return $this->db->query($sql)->result_array();
    }

    public function get_count($table,$count,$where){
        $sql = 'select '.$count.' from '.$this->prefix .$table.' where '.$where;
        return count($this->db->query($sql)->result_array());
    }
    public function get_one_page($page,$pageshow,$pagecount,$table,$where){
        $pagesize = ($page-1) * $pageshow;  
        //计算总页数:  
        $numpages = ceil($pagecount/$pageshow);   //向上取整；
        $sql = "SELECT * FROM ".$this->prefix."$table   where  $where limit $pagesize,$pageshow";
        return $this->db->query($sql)->result_array();
    }


   

    public function gettoday_count_pub_num($where){
        $sql = "select news_id from `fn_lei_zmcx_route_news` where date_format(from_UNIXTIME(`route_addtime`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') AND $where";
        return count($this->db->query($sql)->result_array());
 
    }
    //三表链接查询
    public function get_three_table($get_id,$tablea,$tableb,$on_one,$tablec,$on_two,$where){
        $sql = "select ".$get_id." from ".$this->prefix.$tablea." join " .$this->prefix.$tableb." on ".$on_one." join ".$this->prefix.$tablec." on ".$on_two." where ".$where; 
        return $res = $this->db->query($sql)->row_array();
    }
    public function get_num_count($table,$where,$num){
        $sql = 'select * from '.$this->prefix.$table .' where '.$where.' limit 0,'.$num;
        return $this->db->query($sql)->result_array();
    }

    //前台列表
    public function get_all_list($where, $order, $limit, $ifcount = false)
    {
        if ($ifcount) {
            $sql = "select count(jobs_id) as total from " . $this->db->prefix . "zhou_jobs
		    where $where";
            $res = $this->db->query($sql)->row_array();
            return $res['total'];
        }
        $sql = "select * from " . $this->db->prefix . "zhou_jobs
		where $where order by $order limit $limit";
        $query = $this->db->query($sql)->result_array();
        return $query;
    }

    //前台详情
    public function get_all_show($id)
    {
        if ($id) {
            $sql = "select * from " . $this->prefix . "zhou_jobs
				 where jobs_id=$id";
            return $this->db->query($sql)->row_array();
        }
    }

    public function get_fujia($table){
        $sql = "select * from ".$this->prefix."zhou_$table";
        return $this->db->query($sql)->result_array();
    }

    public function update_hots($id){
        $hots = $this->get_one('zhou_jobs',"jobs_id=$id");
        $sql = "update ".$this->prefix."zhou_jobs set jobs_host = ".($hots[jobs_host]+1)." where jobs_id = $id";
        return $this->db->query($sql);
    }
    
}